<!-- 账号余额页面 -->
<template>
  <div class="balance">
    <div class="fabu">
      <img src="../img/yue.png" />
      <span>账户余额</span>
    </div>
    <div class="yu">
      <h1>余额为：{{ balance }}&emsp;快点来充值吧！</h1>
      <el-button class="charge" type="primary" @click="open">充值</el-button>
    </div>
  </div>
</template>

<script>
import { getByUserId, topUp } from "@/api/balance"
export default {
  name: "accountBalance",
  data() {
    return {
      balance: 40,
    };
  },
  methods: {
    open() {
      this.$prompt("请输入重置金额", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern:/[1-9]\d*/
          ,
        inputErrorMessage: "请输入数字",
      })
        .then(({ value }) => {
          //发送添加余额的后端接口
          this.chargeBalance(value)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    getMyBalance(){
      getByUserId().then((res)=>{
        this.balance = res
      })
    },
    chargeBalance(money){
      topUp(money).then(()=>{
        this.$message({
          type:"success",
          message:"恭喜你成功充值了"+money+"元"
        })
        this.balance+=Number(money)
      })
    }
  },
  created () {
    this.getMyBalance();
  },
};
</script>

<style scoped>
img {
  width: 60px;
  height: 60px;
  position: relative;
  top: 13px;
}
span {
  font-size: 50px;
  color: rgb(163, 220, 243);
}
.fabu {
  text-align: center;
  margin-top: 80px;
}
h1 {
  color: skyblue;
  text-align: center;
}
h1{
  margin-top:80px ;
}
.yu{
  text-align: center;
}
.charge{
  margin-top: 50px !important;
}
</style>